package cn.ac.iscas.service.transformer.dameng;

import cn.ac.iscas.base.utils.SpringUtil;
import cn.ac.iscas.domain.source.DamengSourceColumn;
import cn.ac.iscas.service.queue.dameng.DamengTableQueueService;
import cn.ac.iscas.utils.DamengSqlUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @author LJian
 * @description:
 * @param: "parameters": {
 * "null_columns": [
 * {
 * "column": "column1",
 * "fill_value": "empty",
 * "type": "STRING",
 * "label": "column1_fill"
 * },
 * {
 * "column": "column2",
 * "fill_value": 0,
 * "type": "INTEGER",
 * "label": "column2_fill"
 * }
 * ]
 * }
 * @return:
 * @date: 2022/11/16 14:50
 */
public class DamengFullfilNullTransformer {
    private final static String ID = "fullfil_null";
    private final static String COLUMN_NAME_KEY = "column";
    private final static String LABEL_NAME_KEY = "label";
    private final static String NULL_COLUMNS_NAME_KEY = "null_columns";
    private final static String COLUMN_TYPE_KEY = "type";
    private final static String FILL_VALUE_KEY = "fill_value";
    private String jobId;
    private String componentId;
    private String frontTableName;
    private List<DamengSourceColumn> frontColumns;

    private String currentTableName;
    private List<DamengSourceColumn> currentColumns;

    public DamengFullfilNullTransformer(String jobId, String taskId) {
        this.jobId = jobId;
        this.componentId = taskId;
    }

    public DamengFullfilNullTransformer(String jobId, String taskId, String frontTableName, List<DamengSourceColumn> frontColumns) {
        this.jobId = jobId;
        this.componentId = taskId;
        this.frontTableName = frontTableName;
        this.frontColumns = frontColumns;
        this.currentColumns = frontColumns;
        this.currentTableName = String.format("\"%s\".\"%s_%s\"", jobId, ID, UUID.randomUUID().toString().substring(0,6));

    }


    public DamengFullfilNullTransformer() {

    }

    /*
     * 达梦去重处理
     * Step1.Create临时表
     *     CREATE GLOBAL TEMPORARY TABLE "240102SA"."nulfill_38d540" (  "ADDRESSID" INT NOT NULL ,  "ADDRESS1"  VARCHAR ( 60 )  NOT NULL ,  "ADDRESS2"  VARCHAR ( 60 )   ,  "CITY"  VARCHAR ( 30 )  NOT NULL ,  "POSTALCODE"  VARCHAR ( 15 )  NOT NULL , NOT CLUSTER PRIMARY KEY("ADDRESSID"));
     * Step2.Insert临时表
     *     INSERT INTO "240102SA"."nulfill_38d540" (SELECT * FROM "240102SA"."${pre_table}");
     * Step3.Update临时表，补齐缺失值
     *     UPDATE "240102SA"."nulfill_38d540" SET ADDRESS2 = '空值填充' WHERE LEN(ADDRESS2) IS NULL;
     */
    public List<String> getSql(Map<String, Object> parameters) {
        List<String> sqls = new ArrayList<>();
        String createTempTableSql = DamengSqlUtils.getCreateTemporaryTableSql(this.currentTableName, this.currentColumns);
        String insertTempTableSql = DamengSqlUtils.getInsertFullSql(this.currentTableName, this.frontTableName);

        Map<String, Object> nullFilParameter = parameters;
        String column = nullFilParameter.getOrDefault(COLUMN_NAME_KEY, "").toString();
        //String columnType = nullFilParameter.getOrDefault(COLUMN_TYPE_KEY, "").toString();
        String fillValue = nullFilParameter.getOrDefault(FILL_VALUE_KEY, "").toString();
        //String label = nullFilParameter.getOrDefault(LABEL_NAME_KEY, "").toString();

        String updateTempTableSql = String.format("UPDATE %s SET %s = '%s' WHERE LEN(%s) IS NULL", this.currentTableName, column, fillValue, column);

        sqls.add(createTempTableSql);
        sqls.add(insertTempTableSql);
        sqls.add(updateTempTableSql);
        updateSource();
        return sqls;
    }

    public void updateSource() {
        DamengTableQueueService damengTableQueueService = SpringUtil.getBean(DamengTableQueueService.class);
        Map<String, List<DamengSourceColumn>> tableColumns = new ConcurrentHashMap<>(8);
        tableColumns.put(this.currentTableName, this.currentColumns);
        damengTableQueueService.put(this.jobId, this.componentId, tableColumns);
    }
}
